package com.huisenky.smallidea.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.huisenky.smallidea.common.util.vo.AnnotationInfo;
import lombok.Data;

import javax.annotation.Nonnull;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * date 转化
 *  * bean copy
 *  * map to  funcation
 * 不同类型的tostring
 * class中 field的 key+ 注解的值
 */
@Data
public class BeeUtils {
    /**
     * copu list
     */
    public static <T, E> List<E> copyList(List<T> sourceList, Class<E> clazz){
        List<E> collect = sourceList.stream().map(t -> BeanUtil.copyProperties(t, clazz)).collect(Collectors.toList());
        return collect;
    }

    /**
     * copy bean
     */
    public static <T, E> E copyProperties(T source, Class<E> clazz) {
//        E target = ReflectUtil.newInstance(clazz);
//        return copyProperties(source, target);
        E e = BeanUtil.copyProperties(source, clazz);
        return e;
    }

    public static <T, E> E copyProperties(T source, E target) {
        BeanUtil.copyProperties(source, target);
        return target;
    }

    public static <T, E> E copyProperties(T source, Class<E> clazz, String... ignoreProperties) {
        E target = ReflectUtil.newInstance(clazz);
        BeanUtil.copyProperties(source, target, ignoreProperties);
        return target;
    }

    /**
     *  str转化
     *  String aaa ="{\"name\":\"张三\",\"age\":30}";
     *         Map map = mapStr(aaa, json -> JSON.parseObject(json, Map.class));
     */
    public static <T> T mapStr(String str, Function<String, T> map) {
        try {
            if (StrUtil.isBlank(str)) {
                return null;
            }else {
                return map.apply(str);
            }
        } catch (Exception e) {
            return null;
        }
    }

    /**
     *    获取注解的值
     *    获取注解的 字段的 值 比如  ApiModelProperty 的 value的值
     *    annotationMethod  注解里面的属性值  value等
     */
    public static String getAnnotationComment(Annotation annotation, String annotationMethod) {
        if (annotation == null || StrUtil.isBlank(annotationMethod)) {
            return null;
        }
        try {
            Object annotationVal = ReflectUtil.invoke(annotation, annotationMethod);
            if (annotationVal instanceof String) {
                return (String) annotationVal;
            } else if (annotationVal instanceof String[]) {
                return ((String[]) annotationVal)[0];
            }
        } catch (Exception e) {
            // 忽略
        }
        return null;
    }


    /**
     * 获取class的注解信息
     */
    public static List<AnnotationInfo> getClassAnnotaionInfo(Class<?> clazz, @Nonnull Class<? extends Annotation> annotation, @Nonnull String annotationMethod) {
        Field[] fields = ReflectUtil.getFields(clazz);
        List<AnnotationInfo> header = CollUtil.newArrayList();
        for (Field field : fields) {
            String annotationComment = getAnnotationComment(field.getAnnotation(annotation), annotationMethod);
            AnnotationInfo build = AnnotationInfo.builder().FieldName(field.getName()).annotationClassName(annotation.getName()).annotationMethod(annotationMethod).annotationValue(annotationComment).build();
            header.add(build);
        }
        return header;
    }

    /**
     * 获取各种对象类型的字符串默认值
     * map {} list[]
     */
    public static String defaultValStr(Object obj) {
        if (obj == null) {
            return "";
        } else if (obj instanceof Date) {
            return DateUtil.format((Date) obj, DatePattern.NORM_DATETIME_FORMAT);
        } else {
            return obj.toString();
        }
    }


}
